<!-- <?php echo $moduleTitle?>编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改<?php echo $moduleTitle?>':'添加<?php echo $moduleTitle?>'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
<?php if ($columnList) {?>
  <?php foreach ($columnList as $val) { ?>
    <?php if (isset($val['columnImage'])) {?>

      <el-form-item label="<?php echo $val['columnComment']?>:">
        <uploadImage :limit="1" v-model="form.<?php echo $val['columnName']?>"></uploadImage>
      </el-form-item>
    <?php } elseif (isset($val['columnRow'])) { ?>

      <el-form-item label="<?php echo $val['columnComment']?>:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.<?php echo $val['columnName']?>"
          placeholder="请输入<?php echo $val['columnComment']?>"/>
      </el-form-item>
    <?php } elseif (isset($val['columnValue'])) {?>
      <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>

      <el-form-item label="<?php echo $val['columnComment']?>:">
        <el-radio-group
          v-model="form.<?php echo $val['columnName']?>">
        <?php foreach ($val['columnValue'] as $v) { ?>
          <el-radio :label="<?php echo $v["value"]?>"><?php echo $v["name"]?></el-radio>
        <?php } ?>
        </el-radio-group>
      </el-form-item>
      <?php } elseif ($val['dataType'] == 'bigint' || $val['dataType'] == 'int' || $val['dataType'] == 'smallint' || $val['dataType'] == 'tinyint') {?>

      <el-form-item label="<?php echo $val['columnComment']?>:" prop="<?php echo $val['columnName']?>">
        <el-select
          clearable
          class="ele-block"
          v-model="form.<?php echo $val['columnName']?>"
          placeholder="请选择<?php echo $val['columnComment']?>">
        <?php foreach ($val['columnValue'] as $v) { ?>
          <el-option label="<?php echo $v["name"]?>" :value="<?php echo $v["value"]?>"/>
        <?php } ?>
        </el-select>
      </el-form-item>
      <?php } elseif ($val['dataType'] == 'date' || $val['dataType'] == 'datetime') {?>

      <el-form-item label="<?php echo $val['columnComment']?>:" prop="<?php echo $val['columnName']?>">
        <el-date-picker
          type="datetime"
          class="ele-fluid"
          v-model="form.<?php echo $val['columnName']?>"
          value-format="yyyy-MM-dd hh:mm:ss"
          format="yyyy-MM-dd hh:mm:ss"
          placeholder="请选择<?php echo $val['columnComment']?>"/>
      </el-form-item>
      <?php } ?>
    <?php } else {?>
      <?php if ($val['dataType'] == 'bigint' || $val['dataType'] == 'int' || $val['dataType'] == 'smallint' || $val['dataType'] == 'tinyint') {?>
        
      <el-form-item label="<?php echo $val['columnComment']?>:" prop="<?php echo $val['columnName']?>">
        <el-input-number
          :min="0"
          v-model="form.<?php echo $val['columnName']?>"
          placeholder="请输入<?php echo $val['columnComment']?>"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
      <?php } else {?>
        
      <el-form-item
        label="<?php echo $val['columnComment']?>:"
        prop="<?php echo $val['columnName']?>">
        <el-input
          :maxlength="20"
          v-model="form.<?php echo $val['columnName']?>"
          placeholder="请输入<?php echo $val['columnComment']?>"
          clearable/>
      </el-form-item>
      <?php } ?>
    <?php } ?>
  <?php } ?>
<?php } ?>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: '<?php echo $entityName?>Edit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
<?php if ($columnList) {?>
  <?php foreach ($columnList as $val) { ?>
    <?php if (isset($val['columnValue'])) {?>
        <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>

        <?php echo $val['columnName']?>: [
          {required: true, message: '请选择<?php echo $val['columnComment']?>', trigger: 'blur'}
        ],
        <?php } elseif ($val['dataType'] == 'bigint' || $val['dataType'] == 'int' || $val['dataType'] == 'smallint' || $val['dataType'] == 'tinyint') {?>

        <?php echo $val['columnName']?>: [
          {required: true, message: '请选择<?php echo $val['columnComment']?>', trigger: 'blur'}
        ],
        <?php } elseif ($val['dataType'] == 'date' || $val['dataType'] == 'datetime') {?>

        <?php echo $val['columnName']?>: [
          {required: true, message: '请选择<?php echo $val['columnComment']?>', trigger: 'blur'}
        ],
        <?php } ?>
    <?php } else {?>
        <?php echo $val['columnName']?>: [
          {required: true, message: '请输入<?php echo $val['columnComment']?>', trigger: 'blur'}
        ],
    <?php } ?>
  <?php } ?>
<?php } ?>
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/<?php echo $moduleName?>/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
